package com.mcptt.db.model;

import java.io.Serializable;

import com.cloudptt.api.comm.CheckData;
import com.mcptt.db.DbConst;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;

public class GroupInfo extends AddrInfo implements Serializable
{
	public static final String Group_Id_Root = "0000";
	public static final String Group_Num_All = "000000000";
	public static final String Group_Name_All = "Group_Sys";
	private String groupNum;
	private String parentGroupId;
	private int gCallTimer = 61;



	private int groupLevel = 0;
//	private List<UserInfo> userList = null;

	public boolean isDefaultInfo()
	{
		return this.type == GroupType_Incomplete && 
				name.equals("") && groupNum.equals("");
	}
	
//	public GroupInfo(int type,String name,String groupId,boolean isAffiliation)
//	{
//		super(type,name,groupId);
//		this.groupNum = groupId;
//		if(isAffiliation)
//		{
//			this.attr = 1;
//		}
//		else
//		{
//			this.attr = 0;
//		}
////		this.userCount = 0;
//	}
	public GroupInfo(int type,String name,String groupId,String parentGroupId,int level,int gCallTimer,int groupLevel)
	{
		super(type,name,groupId,level);
		this.groupNum = groupId;
		this.parentGroupId = parentGroupId;
		this.gCallTimer = gCallTimer;
		this.groupLevel = groupLevel;
	}


	public GroupInfo(Cursor c)
	{
		super();
		ContentValues args = new ContentValues();
		DatabaseUtils.cursorRowToContentValues(c, args);
		createFromContentValue(args);
	}
	
	public static GroupInfo getDefaultUserInfo(String defaultGroupNum)
	{
		GroupInfo info = new GroupInfo(Group_Num_All, Group_Name_All);
		if(defaultGroupNum != null
			&& !defaultGroupNum.equals("")
			&& !defaultGroupNum.equals(Group_Num_All))
		{
			info = new GroupInfo(defaultGroupNum, defaultGroupNum);
		}

		return info;
	}

	public static GroupInfo getDefaultUserInfo()
	{
		GroupInfo info = new GroupInfo(Group_Num_All, Group_Name_All);
		return info;
	}
	

//	public GroupInfo() {
//		this.mcId = "";
//		this.type = GroupType_Group;
//		this.name = "";
//		this.groupNum = "";
////		this.userCount = 0;
//	}

	public GroupInfo(String mcId,String name) {
		this.mcId = mcId;
		this.type = GroupType_Default;
		this.name = name;
		this.groupNum = mcId;
//		this.userCount = 0;
	}

	public boolean isSameOne(AddrInfo oldInfo)
	{
		if(oldInfo == null)
		{
			return false;
		}
		String oldMcId = oldInfo.mcId;
		if(this.mcId != null
				&& this.mcId.equals(oldMcId))
		{
			return true;
		}
		return false;
	}

	public boolean isSameOne(String oldNum)
	{
		if(this.groupNum.equals(oldNum))
		{
			return true;
		}
		return false;
	}

	public int getGroupLevel() {
		return groupLevel;
	}

	/**
	 * Pack the object content value to store
	 * 
	 * @return The content value representing the message
	 */
	public ContentValues getContentValues() {
		ContentValues cv = new ContentValues();
		cv.put(DbConst.COL_Id, mcId);
		cv.put(DbConst.COL_Type,type);
		cv.put(DbConst.COL_Name, name);
		cv.put(DbConst.COL_GroupId, groupNum);
		cv.put(DbConst.COL_PGroupId, parentGroupId);
		cv.put(DbConst.COL_Attr, level);
		cv.put(DbConst.COL_GCallTimer, gCallTimer);


		return cv;
	}
	public static ContentValues getUpdateContentValues(String name) 
	{
		ContentValues cv = new ContentValues();
		cv.put(DbConst.COL_Name, name);
		return cv;
	}
	
	public static ContentValues getDefaultGroupInfoContentValues() 
	{
		ContentValues cv = new ContentValues();
		cv.put(DbConst.COL_Type, GroupType_Default);
		return cv;
	}
	
	public static ContentValues getClearDefaultGroupInfoContentValues() 
	{
		ContentValues cv = new ContentValues();
		cv.put(DbConst.COL_Type, GroupType_Normal);
		return cv;
	}
//	public static ContentValues getUpdateContentValues(int userCount) 
//	{
//		ContentValues cv = new ContentValues();
//		cv.put(DbConst.COL_Count, userCount);
//		return cv;
//	}

	public final void createFromContentValue(ContentValues args) {

//		Log.debug(this.getClass(), "createFromContentValue");
		String tmp_s;

		tmp_s = args.getAsString(DbConst.COL_Id);
		if(tmp_s != null) {
			mcId = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_Type);
		if(tmp_s != null) {
			type = CheckData.parseInt(tmp_s);
		}
		tmp_s = args.getAsString(DbConst.COL_Name);
		if(tmp_s != null) {
			name = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_GroupId);
		if(tmp_s != null) {
			groupNum = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_PGroupId);
		if(tmp_s != null) {
			parentGroupId = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_Attr);
		if(tmp_s != null) {
			int value = CheckData.parseInt(tmp_s);
			if(value != -1)
			{
				level = value;
			}
		}
		tmp_s = args.getAsString(DbConst.COL_GCallTimer);
		if(tmp_s != null) {
			int value = CheckData.parseInt(tmp_s);
			if(value != -1)
			{
				gCallTimer = value;
			}
		}
//		tmp_s = args.getAsString(DbConst.COL_Count);
//		if(tmp_s != null) {
//			int value = CheckData.parseInt(tmp_s);
//			if(value != -1)
//			{
//				userCount = value;
//			}
//		}
	}

	public int getUserType() {
		return type;
	}
	
	public void setUserType(int userType) {
		this.type = userType;
	}

	//	public String getuId()
	//	{
	//		return uId;
	//	}
	//	public void setuId(String uId)
	//	{
	//		this.uId = uId;
	//	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}


//	public String getNumDscp()
//	{
//		return convertDscpNum(groupId);
//	}

//	public void setNum(String num)
//	{
//		this.groupNum = num;
//	}
	public String getMcId() {
		return mcId;
	}

	public String getGroupNum() {
		return groupNum;
	}

	public void setGroupNum(String num)
	{
		this.groupNum = num;
	}

	public int getgCallTimer() {
		return gCallTimer;
	}
//	public boolean isAffiliation()
//	{
//		if(attr ==1)
//		{
//			return true;
//		}
//		return false;
//	}
//
//	public void setIsAffiliation(boolean isAffiliatio)
//	{
//		if(isAffiliatio)
//		{
//			attr =1;
//		}
//		else
//		{
//			attr = 0;
//		}
//	}
	
//	public List<UserInfo> getUserList() {
//		return userList;
//	}
//
//	public void setUserList(List<UserInfo> userInfo) {
//		this.userList = userInfo;
//	}

	public String toString()
	{
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("groupLevel = ").append(groupLevel).append(",");
		strBuf.append("type = ").append(type).append(",");
		strBuf.append("mcId = ").append(mcId).append(",");
		strBuf.append("name = ").append(name).append(",");
		strBuf.append("groupId = ").append(groupNum).append(",");
		strBuf.append("parentGroupId = ").append(parentGroupId).append(",");
		strBuf.append("level = ").append(level).append(",");
		strBuf.append("gCallTimer = ").append(gCallTimer).append(".");


		return strBuf.toString();
	}

//	public static String convertDscpNum(String num)
//	{
//		//		if(num.startsWith(Default_CountryCode))
//		//		{
//		//			return num.substring(Default_CountryCode.length() , num.length());
//		//		}
//		return num;
//	}

}
